#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace SgsUI
{
    public static class GameViewUtils
    {
        static object gameViewSizesInstance;
        static MethodInfo getGroup;

        public enum GameViewSizeType
        {
            AspectRatio,FixedResolution
        }

        static GameViewUtils()
        {
            var sizeType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
            var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizeType);
            var instanceProp = singleType.GetProperty("instance");
            getGroup = sizeType.GetMethod("GetGroup");
            gameViewSizesInstance = instanceProp.GetValue(null,null);
        }
    
        public static void GetSize(GameViewSizeGroupType sizeGroupType, int width, int height)
        {
            int dex = FindSize(sizeGroupType, width, height);
            if (dex != -1)
            {
                SetSize(dex);
            }
            else
            {
                GameViewSizeType type = GameViewSizeType.FixedResolution;
                AddCustomSize(type,sizeGroupType,width,height,$"{width}x{height}");
                // Debug.Log("未找到符合条件的分辨率！");
            }
        }
        
        public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height)
        { 
            var group = GetGroup(sizeGroupType);
            var groupType = group.GetType();
            var getBuiltinCount = groupType.GetMethod("GetBuiltinCount");
            var getCustomCount = groupType.GetMethod("GetCustomCount");
            int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);
            var getGameViewSize = groupType.GetMethod("GetGameViewSize");
            var gvsType = getGameViewSize.ReturnType;
            var widthProp = gvsType.GetProperty("width");
            var heightProp = gvsType.GetProperty("height");
            var indexValue = new object[1];
            for (int i = 0; i < sizesCount; i++)
            {
                indexValue[0] = i;
                var size = getGameViewSize.Invoke(group, indexValue);
                int sizeWidth = (int)widthProp.GetValue(size, null);
                int sizeHeight = (int)heightProp.GetValue(size, null);

                if (sizeWidth == width && sizeHeight == height)
                    return i;
            }
            return -1;
        }

        static object GetGroup(GameViewSizeGroupType type)
        {
            return getGroup.Invoke(gameViewSizesInstance,new object[] {(int)type});
        }
    
        public static void SetSize(int index)
        {
            var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
            var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            var gvWnd = EditorWindow.GetWindow(gvWndType);
            selectedSizeIndexProp.SetValue(gvWnd,index, null);
        }

        public static void AddCustomSize(GameViewSizeType viewSizeType,GameViewSizeGroupType sizeGroupType,int width,int height,string text)
        {
            
            var group = GetGroup(sizeGroupType);
            var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize");
            var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
            var ctor = gvsType.GetConstructor(new Type[4] {typeof(int),typeof(int),typeof(int),typeof(string)});
            // 先不用了 这里有个问题 ctor的返回为空 网上也差不多GameViewSize的相关文档
            if(ctor == null)
            {
                Debug.Log("Can't Get ");
                return;
            }
            var newSize = ctor.Invoke(new object[] {(int)viewSizeType,width,height,text});
            addCustomSize.Invoke(group,new object[] {newSize});
        }
    }
}

#endif